//author:jxx
//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
let extension = {
  components: {//动态扩充组件或组件路径
    //表单header、content、footer对应位置扩充的组件
    gridHeader: '',//{ template: "<div>扩展组xx件</div>" },
    gridBody: '',
    gridFooter: '',
    //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
    modelHeader: '',
    modelBody: '',
    modelFooter: ''
  },
  buttons: { view: [], box: [], detail: [] },//扩展的按钮
  // ,"merchantCode": "","merchantName": "",
  methods: {//事件扩展
    // 循环表单并返回数据
    getFormOption (field) {
      let option;
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        })
      })
      return option;
    },
    // 循环表格返回数据
    getColumnsOption (field) {
      let option;
      this.columns.forEach(item => {
        if (item.field == field) {
          option = item;
        }
      })
      return option;
    },
    onInit () {
      // label宽度
      this.boxOptions.labelWidth = 140
      // 弹框宽度
      this.boxOptions.width = 1000
      // 弹框高度
      this.boxOptions.height = 600
      // 开启单选
      //;
      // 内容超出后自动换行
      this.textInline = true;
      // 不显示多选框
      //;
      // 显示序号
      //;
      // 循环表单
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          let field = item.field
          if (field != "isPurchaseLimit" && field != "isRealName" && field != "isHint" && field != "isReserve" && field != "saleName") {
            item.hidden = true
          }
        })
      })

      let riskHints = this.getColumnsOption("riskHint")
      this.$set(riskHints, "show", true)
      let epidemicHints = this.getColumnsOption("epidemicHint")
      this.$set(epidemicHints, "show", true)
      // 限购字段
      let singleId = this.getFormOption('singleId'); //单一ID1 手机号 2 身份证号
      let singleType = this.getFormOption('singleType'); //限购类型 1限购订单数 2 票数  3购买间隔时长
      let singleTicket = this.getFormOption('singleTicket'); //票数
      let singleInterval = this.getFormOption('singleInterval'); //购买间隔时长
      let singleOrder = this.getFormOption('singleOrder'); //限购订单数
      let partiallyVisible = this.getFormOption('partiallyVisible'); //仅部分可见，1会员 2 黑名单 3 白名单 4生日价
      let idCardRestrictions = this.getFormOption('idCardRestrictions'); //身份证判断限制，1地区 2 年龄 3 性别
      let limitArea = this.getFormOption('limitArea'); //限制地区
      let limitAge = this.getFormOption('limitAge'); //限制年龄
      let limitMaxAge = this.getFormOption('limitMaxAge'); //限制年龄
      let limitGender = this.getFormOption('limitGender'); //限制性别
      let leastAdvanceDay = this.getFormOption('leastAdvanceDay'); //最少提前几天
      let minOrder = this.getFormOption('minOrder'); //最小起订量
      let maxOrder = this.getFormOption('maxOrder'); //最大起订量
      // 实名制字段
      let RealNameType = this.getFormOption('RealNameType'); //实名制类型选择
      // 提示字段
      let hintChoose = this.getFormOption('hintChoose'); //提示类型
      let riskHint = this.getFormOption('riskHint'); //风险提示
      let epidemicHint = this.getFormOption('epidemicHint'); //疫情提示
      let quotatype = this.getFormOption('quotaType')
      // 修改title
      // 增加仅部分可见说明
      this.$set(partiallyVisible, "extra", {
        tooltip: true,
        content: "1.选择会员表示仅会员可见;<br/>2.选择黑名单表示黑名单内均不可见;<br/>3.选择白名单表示仅白名单可见;<br/>4.选择生日价仅生日价可见."
      })
      // 是否限购 isPurchaseLimit 绑定change事件
      let isPurchaseLimit = this.getFormOption('isPurchaseLimit'); //是否限购
      idCardRestrictions.hidden = true
      limitGender.hidden = true
      limitAge.hidden = true
      limitMaxAge.hidden = true
      limitArea.hidden = true

      isPurchaseLimit.onChange = (val, item) => {
        // console.log("是否限购", val, item);
        if (val == "0") {
          // 限购类字段隐藏
          this.$set(singleId, "hidden", true)
          this.$set(singleType, "hidden", true)
          this.$set(singleTicket, "hidden", true)
          this.$set(singleInterval, "hidden", true)
          this.$set(singleOrder, "hidden", true)
          this.$set(partiallyVisible, "hidden", true)
          this.$set(idCardRestrictions, "hidden", true)
          this.$set(limitArea, "hidden", true)
          this.$set(limitAge, "hidden", true)
          this.$set(limitMaxAge, "hidden", true)
          this.$set(limitGender, "hidden", true)
          this.$set(leastAdvanceDay, "hidden", true)
          this.$set(minOrder, "hidden", true)
          this.$set(maxOrder, "hidden", true)
          this.$set(quotatype, "hidden", true)
          // 重置限购内数据
          this.$set(singleInterval, "disabled", true)
          this.$set(singleTicket, "disabled", true)
          this.$set(singleOrder, "disabled", true)
          this.$set(partiallyVisible, "disabled", true)
          this.$set(limitGender, "disabled", true)
          this.$set(limitAge, "disabled", true)
          this.$set(limitMaxAge, "disabled", true)
          this.$set(limitArea, "disabled", true)
          this.editFormFields.singleId = ''
          this.editFormFields.singleType = []
          this.editFormFields.singleTicket = ""
          this.editFormFields.singleInterval = ""
          this.editFormFields.singleOrder = ""
          this.editFormFields.partiallyVisible = []
          this.editFormFields.idCardRestrictions = []
          this.editFormFields.limitArea = ""
          this.editFormFields.limitProvince = ""
          this.editFormFields.limitAge = ""
          this.editFormFields.limitMaxAge = ""
          this.editFormFields.limitGender = ""
          this.editFormFields.leastAdvanceDay = ""
          this.editFormFields.minOrder = ""
          this.editFormFields.maxOrder = ""
          this.editFormFields.quotaType = ""
        } else if (val == "1") {
          // 限购字段显示
          this.$set(singleId, "hidden", false)
          this.$set(singleType, "hidden", false)
          this.$set(singleTicket, "hidden", false)
          this.$set(singleInterval, "hidden", false)
          this.$set(singleOrder, "hidden", false)
          this.$set(partiallyVisible, "hidden", false)
          this.$set(leastAdvanceDay, "hidden", false)
          this.$set(minOrder, "hidden", false)
          this.$set(maxOrder, "hidden", false)
          this.$set(quotatype, "hidden", false)
          this.editFormFields.quotaType = "1"
          this.editFormFields.singleId = "1"
          idCardRestrictions.hidden = true
          limitGender.hidden = true
          limitAge.hidden = true
          limitMaxAge.hidden = true
          limitArea.hidden = true
        }
      }
      singleType.onChange = (val, item) => {
        if (val.includes("4")) {
          this.$set(partiallyVisible, "readonly", false)
        } else {
          this.editFormFields.partiallyVisible = ""
          this.$set(partiallyVisible, "readonly", true)
        }
        // 选中购买间隔时长
        if (val.includes("3")) {
          // console.log("选中购买间隔时长")
          this.$set(singleInterval, "disabled", false)
        } else {
          this.editFormFields.singleInterval = ""
          this.$set(singleInterval, "disabled", true)
        }
        // 选中票数
        if (val.includes("2")) {
          // console.log("选中票数")
          this.$set(singleTicket, "disabled", false)
        } else {
          this.editFormFields.singleTicket = ""
          this.$set(singleTicket, "disabled", true)
        }
        // 选中限购订单数
        if (val.includes("1")) {
          // console.log("选中限购订单数")
          this.$set(singleOrder, "disabled", false)
        } else {
          this.editFormFields.singleOrder = ""
          this.$set(singleOrder, "disabled", true)
        }
      }
      // 身份证判断限制 idCardRestrictions 绑定change事件
      idCardRestrictions.onChange = (val, item) => {
        // console.log("身份证判断限制选择", val, item);
        // 选中性别
        if (val.includes("3")) {
          // console.log("选中性别")
          this.$set(limitGender, "disabled", false)
        } else {
          this.editFormFields.limitGender = ""
          this.$set(limitGender, "disabled", true)
        }
        // 选中年龄
        if (val.includes("2")) {
          // console.log("选中年龄")
          this.$set(limitAge, "disabled", false)
          this.$set(limitMaxAge, "disabled", false)
        } else {
          this.editFormFields.limitAge = ""
          this.editFormFields.limitMaxAge = ""
          this.$set(limitAge, "disabled", true)
          this.$set(limitMaxAge, "disabled", true)
        }
        // 选中地区
        if (val.includes("1")) {
          // console.log("选中地区")
          this.$set(limitArea, "disabled", false)
        } else {
          this.editFormFields.limitArea = ""
          this.$set(limitArea, "disabled", true)
        }
      }
      // 是否实名制 isRealName 绑定change事件
      let isRealName = this.getFormOption('isRealName'); //是否实名制
      isRealName.onChange = (val, item) => {
        // console.log("是否实名制", val, item);
        if (val == "0") {
          this.$set(RealNameType, "hidden", true)
          this.editFormFields.RealNameType = []
        } else if (val == "1") {
          // 实名制字段显示
          this.$set(RealNameType, "hidden", false)
        }
      }
      // 是否提示 isHint 绑定change事件
      let isHint = this.getFormOption('isHint'); //是否提示
      isHint.onChange = (val, item) => {
        // console.log("是否提示", val, item);
        if (val == "0") {
          this.$set(hintChoose, "hidden", true)
          this.$set(riskHint, "hidden", true)
          this.$set(epidemicHint, "hidden", true)
          this.$set(riskHint, "disabled", true)
          this.$set(epidemicHint, "disabled", true)
          this.editFormFields.hintChoose = []
          this.editFormFields.riskHint = ""
          this.editFormFields.epidemicHint = ""
        } else if (val == "1") {
          // 提示字段显示
          this.$set(hintChoose, "hidden", false)
          this.$set(riskHint, "hidden", false)
          this.$set(epidemicHint, "hidden", false)
        }
      }
      // 提示类型 hintChoose 绑定change事件
      hintChoose.onChange = (val, item) => {
        // console.log("风险类型选择", val, item);
        this.editFormFields.riskHint = ""
        this.editFormFields.epidemicHint = ""
        if (val == "1") {//风险提示
          // console.log("选中风险");
          this.$set(riskHint, "disabled", false)
          this.$set(epidemicHint, "disabled", true)
        } else if (val == "2") {//疫情提示
          this.$set(riskHint, "disabled", true)
          this.$set(epidemicHint, "disabled", false)
        } else if (val == "3") {//风险、疫情提示
          this.$set(riskHint, "disabled", false)
          this.$set(epidemicHint, "disabled", false)
        }
      }

      singleId.onChange = (v) => {
        if (v == 2 || v == 3) {
          idCardRestrictions.hidden = false
          limitGender.hidden = false
          limitAge.hidden = false
          limitMaxAge.hidden = false
          limitArea.hidden = false
        } else {
          idCardRestrictions.hidden = true
          limitGender.hidden = true
          limitAge.hidden = true
          limitMaxAge.hidden = true
          limitArea.hidden = true
        }
      }
      // 省市
      limitArea.onChange = (val, item) => {
        this.editFormFields.limitProvince = val
      }
    },
    rowClick ({ row, column, event }) { //查询界面table点击行选中当前行
      this.$refs.table.$refs.table.toggleRowSelection(row);
      return true;
    },
    addBefore (formData) {
      return this.veriFromTable(formData)
    },
    updateBefore (formData) {
      return this.veriFromTable(formData)
    },
    // 保存前编辑
    veriFromTable (formData) {
      if (formData.mainData.idCardRestrictions != 1 || formData.mainData.idCardRestrictions != "1") {
        formData.mainData.limitProvince = ""
      } else {
        let list = formData.mainData.limitProvince.split(",")
        formData.mainData.limitProvince = list[0]
        if (list[1] == undefined) {
          formData.mainData.limitArea = ""
        }
      }
      return true
    },

    modelOpenAfter (row) {
      // 限购字段
      let singleId = this.getFormOption('singleId'); //单一ID1 手机号 2 身份证号
      let singleType = this.getFormOption('singleType'); //限购类型 1限购订单数 2 票数  3购买间隔时长
      let singleTicket = this.getFormOption('singleTicket'); //票数
      let singleInterval = this.getFormOption('singleInterval'); //购买间隔时长
      let singleOrder = this.getFormOption('singleOrder'); //限购订单数
      let partiallyVisible = this.getFormOption('partiallyVisible'); //仅部分可见，1会员 2 黑名单 3 白名单 4生日价
      let idCardRestrictions = this.getFormOption('idCardRestrictions'); //身份证判断限制，1地区 2 年龄 3 性别
      let limitArea = this.getFormOption('limitArea'); //限制地区
      let limitAge = this.getFormOption('limitAge'); //限制年龄
      let limitMaxAge = this.getFormOption('limitMaxAge'); //限制年龄
      let limitGender = this.getFormOption('limitGender'); //限制性别
      let leastAdvanceDay = this.getFormOption('leastAdvanceDay'); //最少提前几天
      let minOrder = this.getFormOption('minOrder'); //最小起订量
      let maxOrder = this.getFormOption('maxOrder'); //最大起订量
      // 实名制字段
      let RealNameType = this.getFormOption('RealNameType'); //实名制类型选择
      // 提示字段
      let hintChoose = this.getFormOption('hintChoose'); //提示类型
      let riskHint = this.getFormOption('riskHint'); //风险提示
      let epidemicHint = this.getFormOption('epidemicHint'); //疫情提示
      let quotatype = this.getFormOption('quotaType')
      if (this.currentAction == "Add") {
        this.editFormFields.isPurchaseLimit = 0
        this.editFormFields.isRealName = 0
        this.editFormFields.isHint = 0
        this.editFormFields.isReserve = 0
        // 限购类字段隐藏
        this.$set(singleId, "hidden", true)
        this.$set(singleType, "hidden", true)
        this.$set(singleTicket, "hidden", true)
        this.$set(singleInterval, "hidden", true)
        this.$set(singleOrder, "hidden", true)
        this.$set(partiallyVisible, "hidden", true)
        this.$set(idCardRestrictions, "hidden", true)
        this.$set(limitArea, "hidden", true)
        this.$set(limitAge, "hidden", true)
        this.$set(limitMaxAge, "hidden", true)
        this.$set(limitGender, "hidden", true)
        this.$set(leastAdvanceDay, "hidden", true)
        this.$set(minOrder, "hidden", true)
        this.$set(maxOrder, "hidden", true)
        // 实名制字段隐藏
        this.$set(RealNameType, "hidden", true)
        // 提示字段隐藏
        this.$set(hintChoose, "hidden", true)
        this.$set(riskHint, "hidden", true)
        this.$set(epidemicHint, "hidden", true)
        // 重置限购内数据
        this.$set(singleInterval, "disabled", true)
        this.$set(singleTicket, "disabled", true)
        this.$set(singleOrder, "disabled", true)
        this.$set(limitGender, "disabled", true)
        this.$set(limitAge, "disabled", true)
        this.$set(limitMaxAge, "disabled", true)
        this.$set(limitArea, "disabled", true)
        //  重置提示内数据
        this.$set(riskHint, "disabled", true)
        this.$set(epidemicHint, "disabled", true)
        quotatype.hidden = true
      } else if (this.currentAction == "update") {
        // console.log("编辑", row);
        // 是否限购
        if (row.isPurchaseLimit == "1") {
          // console.log("有限购", row.isPurchaseLimit);
          // 限购字段显示
          this.$set(quotatype, "hidden", false)
          this.$set(singleId, "hidden", false)
          this.$set(singleType, "hidden", false)
          this.$set(singleTicket, "hidden", false)
          this.$set(singleInterval, "hidden", false)
          this.$set(singleOrder, "hidden", false)
          this.$set(partiallyVisible, "hidden", false)
          this.$set(idCardRestrictions, "hidden", false)
          this.$set(limitArea, "hidden", false)
          this.$set(limitAge, "hidden", false)
          this.$set(limitMaxAge, "hidden", false)
          this.$set(limitGender, "hidden", false)
          this.$set(leastAdvanceDay, "hidden", false)
          this.$set(minOrder, "hidden", false)
          this.$set(maxOrder, "hidden", false)
          if (row.limitArea == null || row.limitArea == "") {
            if (row.limitProvince != "" && row.limitProvince != null) {
              this.editFormFields.limitArea = [parseInt(row.limitProvince)]
            }

          }
          // 限购类型包含值
          if (row.singleId == 2 || row.singleId == 3) {
            idCardRestrictions.hidden = false
            limitGender.hidden = false
            limitAge.hidden = false
            limitMaxAge.hidden = false
            limitArea.hidden = false
          } else {
            idCardRestrictions.hidden = true
            limitGender.hidden = true
            limitAge.hidden = true
            limitMaxAge.hidden = true
            limitArea.hidden = true
          }


          if (row.singleType) {
            if (row.singleType.includes("4")) {
              this.editFormFields.partiallyVisible = [row.partiallyVisible]
              this.$set(partiallyVisible, "readonly", false)
            } else {
              this.editFormFields.partiallyVisible = []
              this.$set(partiallyVisible, "readonly", true)
            }
            if (row.singleType.includes("3")) {
              // console.log("选中购买间隔时长")
              this.$set(singleInterval, "disabled", false)
            } else {
              this.editFormFields.singleInterval = ""
              this.$set(singleInterval, "disabled", true)
            }
            // 选中票数
            if (row.singleType.includes("2")) {
              // console.log("选中票数")
              this.$set(singleTicket, "disabled", false)
            } else {
              this.editFormFields.singleTicket = ""
              this.$set(singleTicket, "disabled", true)
            }
            // 选中限购订单数
            if (row.singleType.includes("1")) {
              // console.log("选中限购订单数")
              this.$set(singleOrder, "disabled", false)
            } else {
              this.editFormFields.singleOrder = ""
              this.$set(singleOrder, "disabled", true)
            }
          } else {
            this.editFormFields.singleInterval = ""
            this.$set(singleInterval, "disabled", true)
            this.editFormFields.singleTicket = ""
            this.$set(singleTicket, "disabled", true)
            this.editFormFields.singleOrder = ""
            this.$set(singleOrder, "disabled", true)
          }
          // 身份证判断限制包含值
          if (row.idCardRestrictions) {
            // 选中性别
            if (row.idCardRestrictions.includes("3")) {
              // console.log("选中性别")
              this.$set(limitGender, "disabled", false)
            } else {
              this.editFormFields.limitGender = ""
              this.$set(limitGender, "disabled", true)
            }
            // 选中年龄
            if (row.idCardRestrictions.includes("2")) {
              // console.log("选中年龄")
              this.$set(limitAge, "disabled", false)
              this.$set(limitMaxAge, "disabled", false)
            } else {
              this.editFormFields.limitAge = ""
              this.$set(limitAge, "disabled", true)
              this.editFormFields.limitMaxAge = ""
              this.$set(limitMaxAge, "disabled", true)
            }
            // 选中地区
            if (row.idCardRestrictions.includes("1")) {
              // console.log("选中地区")
              this.$set(limitArea, "disabled", false)
            } else {
              this.editFormFields.limitArea = ""
              this.editFormFields.limitProvince = ""
              this.$set(limitArea, "disabled", true)
            }
          } else {
            this.editFormFields.limitGender = ""
            this.$set(limitGender, "disabled", true)
            this.editFormFields.limitAge = ""
            this.$set(limitAge, "disabled", true)
            this.editFormFields.limitMaxAge = ""
            this.$set(limitMaxAge, "disabled", true)
            this.editFormFields.limitArea = ""
            this.editFormFields.limitProvince = ""
            this.$set(limitArea, "disabled", true)
          }
        } else {
          // 限购类字段隐藏
          this.$set(quotatype, "hidden", true)
          this.$set(singleId, "hidden", true)
          this.$set(singleType, "hidden", true)
          this.$set(singleTicket, "hidden", true)
          this.$set(singleInterval, "hidden", true)
          this.$set(singleOrder, "hidden", true)
          this.$set(partiallyVisible, "hidden", true)
          this.$set(idCardRestrictions, "hidden", true)
          this.$set(limitArea, "hidden", true)
          this.$set(limitAge, "hidden", true)
          this.$set(limitMaxAge, "hidden", true)
          this.$set(limitGender, "hidden", true)
          this.$set(leastAdvanceDay, "hidden", true)
          this.$set(minOrder, "hidden", true)
          this.$set(maxOrder, "hidden", true)
          // 重置限购内数据
          this.$set(singleInterval, "disabled", true)
          this.$set(singleTicket, "disabled", true)
          this.$set(singleOrder, "disabled", true)
          this.$set(limitGender, "disabled", true)
          this.$set(limitAge, "disabled", true)
          this.$set(limitMaxAge, "disabled", true)
          this.$set(limitArea, "disabled", true)
          this.editFormFields.singleId = ''
          this.editFormFields.singleType = []
          this.editFormFields.singleTicket = ""
          this.editFormFields.singleInterval = ""
          this.editFormFields.singleOrder = ""
          this.editFormFields.partiallyVisible = []
          this.editFormFields.idCardRestrictions = []
          this.editFormFields.limitArea = ""
          this.editFormFields.limitProvince = ""
          this.editFormFields.limitAge = ""
          this.editFormFields.limitMaxAge = ""
          this.editFormFields.limitGender = ""
          this.editFormFields.leastAdvanceDay = ""
          this.editFormFields.minOrder = ""
          this.editFormFields.maxOrder = ""
        }
        // 是否实名制
        if (row.isRealName == "1") {
          // 实名制字段显示
          this.$set(RealNameType, "hidden", false)
        } else {
          this.$set(RealNameType, "hidden", true)
          this.editFormFields.RealNameType = ''
        }
        // 是否提示
        if (row.isHint == "1") {
          // 提示字段显示
          this.$set(hintChoose, "hidden", false)
          this.$set(riskHint, "hidden", false)
          this.$set(epidemicHint, "hidden", false)
          // 提示类型包含值
          if (row.hintChoose == "1") {//风险提示
            // console.log("选中风险");
            this.$set(riskHint, "disabled", false)
            this.$set(epidemicHint, "disabled", true)
          } else if (row.hintChoose == "2") {//疫情提示
            this.$set(riskHint, "disabled", true)
            this.$set(epidemicHint, "disabled", false)
          } else if (row.hintChoose == "3") {//风险、疫情提示
            this.$set(riskHint, "disabled", false)
            this.$set(epidemicHint, "disabled", false)
          }
        } else {
          this.$set(hintChoose, "hidden", true)
          this.$set(riskHint, "hidden", true)
          this.$set(epidemicHint, "hidden", true)
          this.$set(riskHint, "disabled", true)
          this.$set(epidemicHint, "disabled", true)
          this.editFormFields.hintChoose = ''
          this.editFormFields.riskHint = ""
          this.editFormFields.epidemicHint = ""
        }
      }
    },
    delBeforeDataAsync (ids, rows) {
      let sellingRulesid = rows.map(i => i.saleCode)
      let obj = {
        url: '/api/distriProducts/getPageData',
        params: {
          order: "desc",
          page: 1,
          rows: 30,
          sort: "createDate",
          wheres: `[{\"name\":\"sellingRulesid\",\"value\":\"${sellingRulesid.join()}\",\"displayType\":\"checkbox\"}]`
        }
      }
      return obj
    }
  }
};
export default extension;
